package com.tsk.state.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.statemachine.StateMachine;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 黄嘉振
 * @create: 2023/5/24 17:56
 * @description:    PAPER
 **/
@RestController
@RequestMapping("/order")
public class OrderController {

//    @Autowired
//    private StateMachine<OrderStatus, OrderStatusChangeEvent> stateMachine;

//    /**
//     * 根据id查询订单
//     *
//     * @return
//     */
//    @RequestMapping("/getById")
//    public Order getById(@RequestParam("id") Long id) {
//        //根据id查询订单
//        Order order = orderService.getById(id);
//        return order;
//    }
//    /**
//     * 创建订单
//     *
//     * @return
//     */
//    @RequestMapping(value = "/create", produces = "application/json;charset=UTF-8")
//    public String create(@RequestParam String orderNumber) {
//        //创建订单
//        // 保证获取状态时唯一
//        Collection<State<OrderStatus, OrderStatusChangeEvent>> states = stateMachine.getStates();
//        System.out.println("states: " + states);
//        StateMachineAccessor<OrderStatus, OrderStatusChangeEvent> stateMachineAccessor = stateMachine.getStateMachineAccessor();
//        System.out.println(stateMachine.sendEvent(OrderStatusChangeEvent.PAYED));
//        System.out.println(stateMachine.sendEvent(OrderStatusChangeEvent.DELIVERY));
//        System.out.println(stateMachine.sendEvent(OrderStatusChangeEvent.DELIVERY));
//        System.out.println(stateMachine.sendEvent(OrderStatusChangeEvent.RECEIVED));
//        return "sucess";
//    }

//    @Autowired
//    private ApplicationContext context;

    @Autowired
    private StatemachineService<OrderStatus, OrderStatusChangeEvent, String> statemachineService;

    @RequestMapping("/test")
    public void test(@RequestParam String id) throws Exception {
        statemachineService.execute(id, OrderStatusChangeEvent.PAYED);
    }

    @RequestMapping("/get")
    public Object get(@RequestParam String id) throws Exception {
        OrderStatus state = statemachineService.getState(id);
        System.out.println("machine state: " + state);
        return state;
    }

//    /**
//     * 对订单进行支付
//     *
//     * @param id
//     * @return
//     */
//    @RequestMapping("/pay")
//    public String pay(@RequestParam("id") Long id) {
//        //对订单进行支付
//        orderService.pay(id);
//        return "success";
//    }
//
//    /**
//     * 对订单进行发货
//     *
//     * @param id
//     * @return
//     */
//    @RequestMapping("/deliver")
//    public String deliver(@RequestParam("id") Long id) {
//        //对订单进行确认收货
//        orderService.deliver(id);
//        return "success";
//    }
//    /**
//     * 对订单进行确认收货
//     *
//     * @param id
//     * @return
//     */
//    @RequestMapping("/receive")
//    public String receive(@RequestParam("id") Long id) {
//        //对订单进行确认收货
//        orderService.receive(id);
//        return "success";
//    }
}
